import 'package:amap_map_fluttify/amap_map_fluttify.dart';
import 'package:flutter/material.dart';
import 'package:flutter_shop/pages/base/base_presenter.dart';
import 'package:flutter_shop/pages/base/base_state.dart';
import 'package:flutter_shop/pages/common/index_page/provider/index_page_provider.dart';
import 'package:flutter_shop/r.dart';
import 'package:flutter_shop/utils/utils.dart';
import 'package:flutter_shop/widget/common_app_bar.dart';
import 'package:provider/provider.dart';

///我的地图
class MyMap extends StatefulWidget {
  @override
  _MyMapState createState() => _MyMapState();
}

class _MyMapState extends BaseState<MyMap, BasePresenter> {
  AmapController _controller;
  int _type = 0;
  List<Marker> _markers = [];

  List<LatLng> _latLngList = [];
  List<Polygon> _polygons = [];

  @override
  Widget build(BuildContext context) {
    LatLng latLng = Provider.of<IndexPageProvider>(context).latLng;

    return Scaffold(
      appBar: CommonAppBar("我的地图"),
      body: Container(
          width: double.infinity,
          height: double.infinity,
          child: Column(
            children: <Widget>[
              Container(
                alignment: Alignment.centerLeft,
                child: Wrap(
                  children: <Widget>[
                    RaisedButton(
                      onPressed: () {
                        showToast("添加Mark");
                        setState(() {
                          _type = 0;
                        });
                      },
                      child: Text("添加Mark"),
                    ),
                    RaisedButton(
                      onPressed: () {
                        _deleteMark();
                      },
                      child: Text("删除Mark"),
                    ),
                    RaisedButton(
                      onPressed: () {
                        showToast("绘制多边形");
                        setState(() {
                          _latLngList.clear();
                          _type = 1;
                        });
                      },
                      child: Text("绘制多边形"),
                    ),
                    RaisedButton(
                      onPressed: () {
                        _deletePolygon();
                      },
                      child: Text("删除多边形"),
                    ),
                  ],
                ),
              ),
              Expanded(
                  child: AmapView(
                // 地图类型 (可选)
                mapType: MapType.Standard,
                // 是否显示缩放控件 (可选)
                showZoomControl: true,
                // 是否显示指南针控件 (可选)
                showCompass: true,
                // 是否显示比例尺控件 (可选)
                showScaleControl: true,
                // 是否使能缩放手势 (可选)
                zoomGesturesEnabled: true,
                // 是否使能滚动手势 (可选)
                scrollGesturesEnabled: true,
                // 是否使能旋转手势 (可选)
                rotateGestureEnabled: true,
                // 是否使能倾斜手势 (可选)
                tiltGestureEnabled: true,
                // 缩放级别 (可选)
                zoomLevel: 15,
                // 中心点坐标 (可选)
                centerCoordinate: latLng,
                // 标记 (可选)
                markers: <MarkerOption>[],
                // 标识点击回调 (可选)
                onMarkerClicked: (Marker marker) {},
                // 地图点击回调 (可选)
                onMapClicked: (LatLng coord) async {
                  if (_type == 0) {
                    _addWidgetMark(coord);
                  } else if (_type == 1) {
                    _addPolygon(coord);
                  }
                },
                // 地图拖动开始 (可选)
                onMapMoveStart: (MapMove move) {},
                // 地图拖动结束 (可选)
                onMapMoveEnd: (MapMove move) {},
                // 地图创建完成回调 (可选)
                onMapCreated: (controller) async {
                  _controller = controller;
                  await controller.showMyLocation(MyLocationOption(show: true));
                },
              )),
            ],
          )),
    );
  }

  ///添加控件mark
  void _addWidgetMark(LatLng coord) async {
    final marker = await _controller?.addMarker(
      MarkerOption(
        latLng: coord,
        widget: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Text('自定义Mark'),
            Image.asset(
              R.assetsImgLogo,
              width: 30,
              height: 30,
            )
          ],
        ),
        imageConfig: createLocalImageConfiguration(context),
        title: '我是自定义WidgetMarker',
        snippet: '6666666',
      ),
    );
    _markers.add(marker);
  }

  ///删除mark
  void _deleteMark() {
    _markers.forEach((mark) {
      mark.remove();
    });
    setState(() {
      _markers.clear();
    });
  }

  ///添加多边形
  void _addPolygon(LatLng coord) async {
    _latLngList.add(coord);
    if (_latLngList.length < 4) {
      _addWidgetMark(coord);
      Utils.showToast("再选择${4 - _latLngList.length}个点");
      return;
    }

    final polygon = await _controller?.addPolygon(PolygonOption(
      latLngList: _latLngList,
      width: 10,
      strokeColor: Colors.red,
    ));
    _polygons.add(polygon);

    _latLngList.clear();
  }

  ///删除mark
  void _deletePolygon() {
    _polygons.forEach((polygon) {
      polygon.remove();
    });
    setState(() {
      _polygons.clear();
    });

    _deleteMark();
  }
}
